package com.wze.wze.Controller;

import com.wze.wze.Aop.PasToken;
import com.wze.wze.Entity.Movie;
import com.wze.wze.Entity.Music;
import com.wze.wze.Entity.ResultEntity;
import com.wze.wze.Service.MovieService;
import com.wze.wze.Util.JSONTool;
import com.wze.wze.Util.OSSUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;
import java.util.UUID;

@CrossOrigin //解决跨域
@RestController //控制器注解
@RequestMapping("/Movie/")//请求路径
public class MovieController {
    @Resource
    MovieService movieService;
    ResultEntity resultEntity = null;

    @PostMapping("Movie")
    private String addMv(@RequestParam("mfile") MultipartFile MovieFile, @RequestParam("ifile") MultipartFile MImageFile, @RequestParam("mid") int mid,
                         @RequestParam("MvName") String MvName, @RequestParam("Mvt") String Mvt, @RequestParam("MvInfo") String MvInfo) {
        resultEntity = new ResultEntity();
        OSSUtil ossUtil = new OSSUtil();
        String FilePath="D:\\BiShe\\";//存储Excel的位置
        String SuffixName1 = MovieFile.getOriginalFilename().substring(MovieFile.getOriginalFilename().lastIndexOf("."));//获取文件后缀
        String FileNewName1= UUID.randomUUID().toString()+SuffixName1;//新的文件名
        File dest1 = new File(FilePath+FileNewName1);//生成新的文件
        String SuffixName2 = MImageFile.getOriginalFilename().substring(MImageFile.getOriginalFilename().lastIndexOf("."));//获取文件后缀
        String FileNewName2= UUID.randomUUID().toString()+SuffixName2;//新的文件名
        File dest2 = new File(FilePath+FileNewName2);//生成新的文件
        if(!dest1.getParentFile().exists() && dest2.getParentFile().exists()){//如果路径不存在，就生成
            dest1.getParentFile().mkdirs();
            dest2.getParentFile().mkdirs();
        }
        try{
            MovieFile.transferTo(dest1);//将临时文件保存到对应的路径
            MImageFile.transferTo(dest2);//将临时文件保存到对应的路径
        }catch (Exception e){
            e.printStackTrace();
        }
        String OSSFilePath1 = ossUtil.updateFile(FilePath+FileNewName1,FileNewName1);
        String OSSFilePath2 = ossUtil.updateFile(FilePath+FileNewName2,FileNewName2);
            try {
                if (movieService.addMovie(OSSFilePath1,OSSFilePath2,mid,MvName,Mvt,MvInfo) > 0) {
                    resultEntity.setCode("0");
                    resultEntity.setMsg("添加成功！");
                } else {
                    resultEntity.setCode("1");
                    resultEntity.setMsg("添加失败！");
                }
            } catch (Exception e) {
                e.printStackTrace();
                resultEntity.setCode("9999");
                resultEntity.setMsg("系统错误，请联系管理员");
            }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    @GetMapping("mMovie")
    private String getMvBymId(@RequestParam("mid") int mid) {
        resultEntity = new ResultEntity();
        try {
            Movie movie  = movieService.getMovieBymid(mid);
            if (movie == null) {
                resultEntity.setCode("1");
                resultEntity.setMsg("查无此音乐");
            } else {
                resultEntity.setCode("0");
                resultEntity.setMsg("查询成功");
                resultEntity.setData(movie);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);

    }

    @GetMapping("Movie")
    private String getMvById(@RequestParam("id") int id) {
        resultEntity = new ResultEntity();
        try {
            Movie movie  = movieService.getMovieByid(id);
            if (movie == null) {
                resultEntity.setCode("1");
                resultEntity.setMsg("查无此音乐");
            } else {
                resultEntity.setCode("0");
                resultEntity.setMsg("查询成功");
                resultEntity.setData(movie);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);

    }


    @DeleteMapping("Movie")
    private String delMv(@RequestParam("id") int id) {
        resultEntity = new ResultEntity();
        try {
            if (movieService.delMovieById(id)> 0) {
                resultEntity.setCode("0");
                resultEntity.setMsg("删除成功！");
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("删除失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    @PasToken
    @GetMapping("MovieList")
    private String MovieList(
            @RequestParam("page") int page,
            @RequestParam("limit") int limit,
            @RequestParam("MvNAme") String MvName,
            @RequestParam("id") int id
    ){
        try {
            resultEntity = new ResultEntity();
            int Start = (page-1) * limit;//计算分页位置
            List<Movie> list = movieService.getMovieList(Start, limit,id,MvName);
            resultEntity.setCode("0");
            resultEntity.setData(list);
            resultEntity.setMsg("查询成功!");
        }catch (Exception e){
            resultEntity.setCode("1");
            resultEntity.setMsg("查询失败!"+e.getMessage());
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }

}
